package com.wyhello.tube4.spring.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wyhello.tube4.pojo.Bag;
import com.wyhello.tube4.pojo.Reside;
import com.wyhello.tube4.pojo.Working;
import com.wyhello.tube4.spring.model.BagRecordsModel;
import com.wyhello.tube4.spring.model.WorkingRecordsModel;
import com.wyhello.tube4.util.DateUtil;
@Controller
public class WorkingRecordsController {
	@Autowired
	private WorkingRecordsModel workingRecordsModel;
	
	@Autowired
	private BagRecordsModel bagRecordsModel;
	
	/**
	 * 根据锅炉ID查询运行记录
	 * @return
	 */
	@RequestMapping("/working/queryWorking")
	public ModelAndView queryWorking(){
		List<Working> workings = workingRecordsModel.queryWorking();
		
		for (Working working : workings) {
			if(DateUtil.date2Str(working.getEndTime(), "yyyy-MM-dd HH:mm:ss")==null){
				String stime = String.valueOf(working.getStartTime().getTime());
				String etime = String.valueOf(new Date().getTime());
				float usetime = (Float.parseFloat(etime) - Float.parseFloat(stime))/2592000000L;
				float useTime = (float)(Math.round(usetime*100))/100;
				working.setUseTime(useTime);
			}
		}
		List<Reside> boilers =  workingRecordsModel.queryResideByBoiler();
		for (Reside reside : boilers) {
			reside.setDescription(Boolean.toString(workingRecordsModel.queryNoWorkingEndByResideId(reside.getId())));
		}
		ModelAndView model = new ModelAndView("main","workings",workings);
		model.addObject("boilers", boilers);
		return model;
	}
	/**
	 * 添加指定锅炉运行记录
	 * @param resideId
	 * @param working
	 * @return
	 */
	@RequestMapping("/working/saveWorking")
	public String saveWorking(int resideId, String sTime, String eTime, Working working){
		Reside reside = workingRecordsModel.getResideById(resideId);
		if(eTime != null & eTime!=""){
			working.setEndTime(DateUtil.str2Date(eTime, null));
			String etime = String.valueOf(DateUtil.str2Date(eTime, null).getTime());
			String stime = String.valueOf(DateUtil.str2Date(sTime, null).getTime());
			float usetime = (Float.parseFloat(etime) - Float.parseFloat(stime))/2592000000L;
			float useTime = (float)(Math.round(usetime*100))/100;
			working.setUseTime(useTime);
			Bag bag = new Bag();
			bag.setName(DateUtil.date2Str(working.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
			bag.setDescription("启动次数记录");
			bag.setReside(reside);
			bagRecordsModel.saveBag(bag);
		}
		working.setStartTime(DateUtil.str2Date(sTime, null));
		working.setReside(reside);
		workingRecordsModel.saveWorking(working);
		return "success";
	}
	@RequestMapping("/working/endWorking")
	public String endWorking(int resideId, String eTime){
		List<Working> workings = workingRecordsModel.queryWorkingByResideId(resideId);
		Working working = workings.get(0);
		
		working.setEndTime(DateUtil.str2Date(eTime, null));
		String etime = String.valueOf(DateUtil.str2Date(eTime, null).getTime());
		String stime = String.valueOf(working.getStartTime().getTime());
		float usetime = (Float.parseFloat(etime) - Float.parseFloat(stime))/2592000000L;
		float useTime = (float)(Math.round(usetime*100))/100;
		working.setUseTime(useTime);
		Bag bag = new Bag();
		bag.setName(DateUtil.date2Str(working.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
		bag.setDescription("启动次数记录");
		bag.setReside(working.getReside());
		bagRecordsModel.saveBag(bag);
		workingRecordsModel.updateWorking(working);
		return "success";
	}
	/**
	 * 删除运行记录
	 * @param request
	 * @return
	 */
	@RequestMapping("/working/deletereside")
	public String delete(HttpServletRequest request){
		String ids = request.getParameter("deletid");
		workingRecordsModel.deleteWorking("("+ids+")"); 
		return "success";
	}
	/**
	 * 添加运行记录前查询锅炉信息
	 * 查询锅炉的信息
	 * @param reside
	 * @return
	 */
	@RequestMapping("/working/queryResideAdd")
	public ModelAndView queryResideAdd(String boilersId){
		Reside boiler =  workingRecordsModel.getResideById(Integer.parseInt(boilersId));
		ModelAndView model = new ModelAndView("add","boiler",boiler);
		return model;
	}/**
	 * 停止运行记录前查询锅炉信息
	 * 查询锅炉的信息
	 * @param reside
	 * @return
	 */
	@RequestMapping("/working/queryResideEnd")
	public ModelAndView queryResideEnd(String boilersId){
		Reside boiler =  workingRecordsModel.getResideById(Integer.parseInt(boilersId));
		ModelAndView model = new ModelAndView("stop","boiler",boiler);
		return model;
	}
}
